﻿#include "AddressController.h"
#include "EditAddressController.h"

AddressController::AddressController(){}

AddressController::~AddressController(){}

void AddressController::viewDidLoad(){
	CAView* view=this->getView();
	CAView* subview=getSubview();
	subview->addSubview(getLabel("ID"));
	addressId=getTextField();
	subview->addSubview(addressId);
	view->addSubview(subview);
	subview=getSubview();
	CAButton* btn=CAButton::createWithLayout(DLayout(DHorizontalLayout_L_W(40, 180), DVerticalLayout_T_H(0, 80)), CAButton::Type::RoundedRect);
	btn->setTitleFontSize(50);
	btn->setTitleForState(CAControl::State::Normal, "查 询");
	btn->setTitleColorForState(CAControl::State::Normal, CAColor4B::WHITE);
	btn->setBackgroundImageForState(CAControl::State::Normal, CAImage::createWithColor4B(themeColor), true);
	btn->addTarget([=]{
		page=1;
		string paras=obj2str("userId=%u&addressId=%s", userId, addressId->getText().c_str());
		sendHttp(view, url, paras, CALLBACK_BIND_1(AddressController::fillData, this));
	}, CAButton::Event::TouchDown);
	subview->addSubview(btn);
	btn=CAButton::createWithLayout(DLayout(DHorizontalLayout_R_W(40, 200), DVerticalLayout_T_H(0, 80)), CAButton::Type::RoundedRect);
	btn->setTitleFontSize(50);
	btn->setTitleForState(CAControl::State::Normal, "新 建");
	btn->setTitleColorForState(CAControl::State::Normal, CAColor4B::WHITE);
	btn->setBackgroundImageForState(CAControl::State::Normal, CAImage::createWithColor4B(themeColor), true);
	btn->addTarget([&]{
		getRootWindow()->pushViewController(EditAddressController::create(0), true);
	}, CAButton::Event::TouchDown);
	subview->addSubview(btn);
	view->addSubview(subview);
	//创建CATableView
	table=CATableView::createWithLayout(DLayout(DHorizontalLayoutFill, DVerticalLayout_T_B(200, 0)));
	//是否开启cell选择
	table->setAllowsSelection(true);
	//是否可以多选cell
	table->setAllowsMultipleSelection(false);
	//是否显示滚动条
	table->setShowsScrollIndicators(false);
	//设置尾部的高度
	table->setTableFooterHeight(100);
	view->addSubview(table);
	//设置加载的cell个数
	table->onNumberOfRowsAtIndexPath([&](unsigned int section){
		return datas.size();
	});
	//设置cell高度
	table->onCellHeightAtIndexPath([](unsigned int section, unsigned int row){
		return 130;
	});
	//设置cell显示的内容
	table->onCellAtIndexPath([&](DSize cellSize, unsigned int section, unsigned int row){
		CATableViewCell* cell=dynamic_cast<CATableViewCell*>(table->dequeueReusableCellWithIdentifier("cell"));
		if(cell==nullptr){
			cell=CATableViewCell::create("cell");

			CALabel* txt=CALabel::createWithLayout(DLayout(DHorizontalLayout_L_W(10, 280), DVerticalLayoutFill));
			txt->setTag(1);
			txt->setFontSize(30);
			txt->setTextAlignment(CATextAlignment::Left);
			txt->setVerticalTextAlignmet(CAVerticalTextAlignment::Center);
			cell->getContentView()->addSubview(txt);

			txt=CALabel::createWithLayout(DLayout(DHorizontalLayout_W_C(200, 0.85), DVerticalLayoutFill));
			txt->setTag(2);
			txt->setFontSize(30);
			txt->setTextAlignment(CATextAlignment::Left);
			txt->setVerticalTextAlignmet(CAVerticalTextAlignment::Center);
			cell->getContentView()->addSubview(txt);

			CAButton* btn=CAButton::createWithLayout(DLayout(DHorizontalLayout_R_W(0, 130), DVerticalLayoutFill), CAButton::Type::Custom);
			btn->setTag(3);
			btn->setBackgroundImageForState(CAControl::State::Normal, CAImage::create("img/button_delete.png"), false);
			cell->insertSubview(btn, -1);
		}
		cell->setDraggingLength(130);

		CSJson::Value val=datas[row];
		CALabel* txt=(CALabel*)cell->getContentView()->getSubviewByTag(1);
		txt->setText(val["name"].asCString());

		txt=(CALabel*)cell->getContentView()->getSubviewByTag(2);
		txt->setText(val["phone"].asCString());

		CAButton* btn=(CAButton*)cell->getSubviewByTag(3);
		btn->addTarget([=]{
			CAAlertView* alert=CAAlertView::create("删除地址信息", "确定要继续该操作吗", {"取消", "确定"});
			alert->show([&](int index){
				CC_RETURN_IF(index==0);
				delRow=row;
				CSJson::Value paras;
				paras["addressId"]=val["addressId"].asUInt();
				paras["userId"]=val["userId"].asUInt();
				CSJson::FastWriter writer;
				sendHttp(this->getView(), "/api/appAddress/deleteById", writer.write(paras), CALLBACK_BIND_1(AddressController::delData, this), CAHttpRequest::Type::Post, true);
			});
		}, CAButton::Event::TouchDown);

		return cell;
	});
	//选中cell时触发
	table->onDidSelectCellAtIndexPath([&](unsigned int section, unsigned int row){
		getRootWindow()->pushViewController(EditAddressController::create(datas[row]["addressId"].asUInt()), true);
	});
}

/**
* 显示时被调用 一般重新加载数据时使用
*/
void AddressController::viewDidAppear(){
	getRootWindow()->setNavigationBarHidden(false, false);
	this->setNavigationBarItem(CANavigationBarItem::create("管理地址"));
	sendHttp(this->getView(), url, obj2str("userId=%u", userId), CALLBACK_BIND_1(AddressController::fillData, this));
}

void AddressController::fillData(const string& data){
	CSJson::Reader reader;
	CSJson::Value value;
	bool flag=reader.parse(data, value);
	if(!flag){
		getAlert(this->getView(), "数据解析异常, 请稍后重试");

		return;
	}

	CSJson::Value val=value["content"];
	int length=value["totalElements"].asInt();
	if(length==0){
		//分页按钮
		datas=val;
		CAView* view=CAView::createWithLayout(DLayoutFill);
		CAImageView* img=CAImageView::createWithLayout(DLayout(DHorizontalLayout_W_C(450, 0.5), DVerticalLayout_T_H(50, 450)));
		img->setImage(CAImage::create("img/nodata.png"));
		view->addSubview(img);
		//添加尾部视图
		table->setTableFooterView(view);
		table->reloadData();

		return;
	}
	if(page==1){
		datas=val;
		table->setTableFooterView(nullptr);
	}else{
		for(int i=0; i<length; i++){
			datas.append(val[i]);
		}
	}
	if(table->getTableFooterView()==nullptr){
		//分页按钮
		CAView * view=CAView::createWithLayout(DLayoutFill);
		CAButton* btn=CAButton::createWithLayout(DLayout(DHorizontalLayout_L_R(100, 100), DVerticalLayout_T_H(40, 100)), CAButton::Type::RoundedRect);
		btn->setTitleFontSize(80);
		btn->setTitleForState(CAControl::State::Normal, "下一页");
		btn->setTitleColorForState(CAControl::State::Normal, CAColor4B::WHITE);
		btn->setBackgroundImageForState(CAControl::State::Normal, CAImage::createWithColor4B(themeColor), true);
		btn->addTarget([&]{
			char ch[10];
			string paras=obj2str("userId=%u&addressId=%s", userId, addressId->getText().c_str());
			sendHttp(this->getView(), url, paras, CALLBACK_BIND_1(AddressController::fillData, this));
		}, CAButton::Event::TouchDown);
		view->addSubview(btn);
		//添加尾部视图
		table->setTableFooterHeight(180);
		table->setTableFooterView(view);
	}
	table->reloadData();
}

void AddressController::delData(const string& data){
	int j=-1;
	int size=datas.size();
	CSJson::Value values;
	values.resize(size-1);
	for(int i=0; i<size; i++){
		if(i==delRow){
			continue;
		}else{
			values[++j]=datas[i];
		}
	}
	if(size>2){
		datas=values;
		table->reloadData();
	}else{
		this->viewDidAppear();
	}
}